<?php

class AjaxController extends BaseController {


    public function postDskhoathi() {
        $hoi_dong_thi = Input::get('hoi_dong_thi');
        $results = DB::table('khoa_this')
                ->whereIn('HoiDongThi_Id', $hoi_dong_thi)
                ->orderBy('ma_khoa_thi', 'asc')
                ->get();
        $html = '<option value="All" id="all_khoa_thi" onclick="selectallkhoathi()">Tất Cả</option>';
        foreach ($results as $results) {
            $html .= '<option value="' . $results->id . '" >' . $results->ma_khoa_thi . '</option>';
        }
        echo $html;
    }

    public function postDeletechungchi() {
        $id_chung_chi_ar = Input::get('id_chung_chi_ar');
        foreach ($id_chung_chi_ar as $id_chung_chi_ar) {
            DB::table('chung_chis')->where('id', $id_chung_chi_ar)->delete();
        }
    }

    public function postDeletehocvien() {
        $id_hoc_vien_ar = Input::get('id_hoc_vien_ar');
        foreach ($id_hoc_vien_ar as $id_hoc_vien_ar) {
            DB::table('hoc_viens')->where('id', $id_hoc_vien_ar)->delete();
        }
    }

    public function postDeletekhoathi() {
        $id_khoa_thi_ar = Input::get('id_khoa_thi_ar');
        foreach ($id_khoa_thi_ar as $id_khoa_thi_ar) {
            DB::table('khoa_this')->where('id', $id_khoa_thi_ar)->delete();
        }
    }

    public function postDeletehoidongthi() {
        $id_hoi_dong_thi_ar = Input::get('id_hoi_dong_thi_ar');
        foreach ($id_hoi_dong_thi_ar as $id_hoi_dong_thi_ar) {
            DB::table('hoi_dong_this')->where('id', $id_hoi_dong_thi_ar)->delete();
        }
    }

    public function postUpconvertpost(){
        $id = Input::get('id');
        $thu_tu = DB::table('bai_viets')->where('id',$id)->pluck('thu_tu_bai_viet');
        $id_temp = 0;
        $thu_tu_temp = 0;
        $bai_viet_up = DB::table('bai_viets')
                    ->where('thu_tu_bai_viet','>',$thu_tu)->orderBy('thu_tu_bai_viet','asc')
                    ->limit(1)
                    ->get();
        foreach ($bai_viet_up as $bai_viet_up) {
            $id_temp = $bai_viet_up->id;
            $thu_tu_temp = $bai_viet_up->thu_tu_bai_viet;
        }
        if($id_temp != 0){
            DB::table('bai_viets')->where('id',$id)->update(array('thu_tu_bai_viet'=>$thu_tu_temp));
            DB::table('bai_viets')->where('id',$id_temp)->update(array('thu_tu_bai_viet'=>$thu_tu));
        }

        var_dump($id);
    }

    public function postDownconvertpost(){
        $id = Input::get('id');
        $thu_tu = DB::table('bai_viets')->where('id',$id)->pluck('thu_tu_bai_viet');
        $id_temp = 0;
        $thu_tu_temp = 0;
        $bai_viet_up = DB::table('bai_viets')
                    ->where('thu_tu_bai_viet','<',$thu_tu)
                    ->orderBy('thu_tu_bai_viet','desc')
                    ->limit(1)                    
                    ->get();
        foreach ($bai_viet_up as $bai_viet_up) {
            $id_temp = $bai_viet_up->id;
            $thu_tu_temp = $bai_viet_up->thu_tu_bai_viet;
        }
        if($id_temp != 0){
            DB::table('bai_viets')->where('id',$id)->update(array('thu_tu_bai_viet'=>$thu_tu_temp));
            DB::table('bai_viets')->where('id',$id_temp)->update(array('thu_tu_bai_viet'=>$thu_tu));
        }
        var_dump($id);
    }
    public function postCheckusername()
    {
        $result = DB::table('tai_khoans')->where('username',Input::get('username'))->count();
        return $result;
    }

    public function postUpdateavatar()
    {
        $new_url                 = Input::get('img_new_url');
        $tai_khoan               = Tai_khoan::find(Auth::user()->id);
        $tai_khoan->anh_dai_dien = $new_url;
        $tai_khoan->save();
        return $new_url;
    }

    public function postGetcourses(){
        $id = Input::get('id');
        $results = DB::table('courses')->where('id','=',$id)->get();
        return $results;
    }

    public function postStorebaiviet()
    {
        $input['tieu_de'] =Input::get('tieu_de');
        $input['tom_tat'] =Input::get('tom_tat');
        $input['ChuyenMucBaiViet_Id'] =Input::get('ChuyenMucBaiViet_Id');
        $input['noi_dung'] =Input::get('noi_dung');
        $bai_viet = new Bai_viet();
        $thu_tu = $bai_viet->get_max_thutu();

        $input['thu_tu_bai_viet'] =  $thu_tu+1;
        $input['TaiKhoan_Id'] = Auth::user()->id;
        $validation = Validator::make($input, Bai_viet::$rules);
        DB::table('bai_viets')->insert($input);
        
    }

    public function postUpdatecourses()
    {
        $id_course =Input::get('id_course');
        DB::table('courses')->where('id','=',$id_course)->update(array('post'=>'1'));
        return 1;

    }
    public function postUpdateschedule()
    {
        $id_schedule =Input::get('id_schedule');
        DB::table('schedules')->where('id','=',$id_schedule)->update(array('post'=>'1'));
        return 1;
    }

    public function postGetschedule(){
        $id = Input::get('id');
        $results = DB::table('schedules')->where('id','=',$id)->get();
        return $results;
    }

    public function postGetuser(){
        $id = Input::get('id');
        $results = DB::table('tai_khoans')->where('id',$id)->get();
        return $results;
    }

    public function postRegistercourses()
    {
        $input = Input::all();
        DB::table('enroll_courses')->insert(
            array(
                'user_id'     => Auth::user()->id,
                'course_id'   => $input['id'],
                'fee_courses' => $input['fee']
                )
        );
        return 1;
    }
    public function postRegisterschedule()
    {
        $input = Input::all();
        DB::table('enroll_events')->insert(
            array(
                'user_id'     => Auth::user()->id,
                'event_id'   => $input['id']
                )
        );
        return 1;
    }

    public function postDelenrollcourse()
    {
        $id = Input::get('id');
        DB::table('enroll_courses')->where('id',$id)->delete();
        return 1;
    }

    public function postDelenrollevent()
    {
        $id = Input::get('id');
        DB::table('enroll_events')->where('id',$id)->delete();
        return 1;
    }

}
